package com.base.cn.platform.os.controller.web.classes.member;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Controller
@RequestMapping("/web/classes/member")
public class WebClassesMemberController extends WebBaseController {

    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 加载班级成员列表
     *
     * @param classesId 班级id
     * @return 班级成员列表
     */
    @RequestMapping("/ajaxMemberList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String, Object> ajaxMemberList(@RequestParam("classesId") BigDecimal classesId) {
        if (DataUtil.idIsNotNull(classesId)) {
            Map<String, Object> condition = Collections.singletonMap("classesId", classesId);
            List<Map<String, Object>> memberList = classesMemberService.findClassesMemberList(condition, true, 1, 1, 2, 3);
            memberList.forEach((v) -> this.removeFields((Map<String, Object>) v.getOrDefault("user", Collections.emptyMap()), "mobile", "email", "password"));
            Map<String, Object> data = Collections.singletonMap("list", memberList);
            return ResultUtil.SUCCESS(data);
        }
        return ResultUtil.ERROR("参数错误");
    }

    private void removeFields(Map<String, Object> map, String... fields) {
        if (fields.length > 0) {
            Stream.of(fields).forEach(map::remove);
        }
    }

    /**
     * 加入班级
     *
     * @param classesId 班级id
     * @return 加入结果
     */
    @RequestMapping("/join")
    @ResponseBody
    public Map<String, Object> join(HttpServletRequest request,
                                    @RequestParam("classesId") BigDecimal classesId,
                                    @RequestParam("requestURL") String requestURL) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        if (DataUtil.isPositive(userId)) {
            customCacheChannel.set(J2CacheRegion.THIRD_MINUTE_REGION.getKey(),CacheConstants.USER_BUY_COURSE_JOIN_CLASS+userId.toString(),requestURL);
            return classesMemberService.saveClassesMemberBatch(classesId, String.valueOf(userId.intValue()), 1, 1);
        }
        return ResultUtil.ERROR("未登录");
    }
}
